<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>docker结构 | 静待花开</title><meta name="author" content="brad 随风起舞"><meta name="copyright" content="brad 随风起舞"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="你机器上的docker分为两部分:一个事带有RESTful API的守护线程(daemon)和一个和daemon联系的客户端.下图展示了运行docker客户端和守护线程(daemon)  RESTful API使用标准的HTTP请求方式例如get,post,delete,等等.  您调用Docker客户端以从守护程序获取信息或向守护程序提供指令；守护程序是一个服务器，它使用HTTP协议接收请求并从">
<meta property="og:type" content="article">
<meta property="og:title" content="docker结构">
<meta property="og:url" content="https://zhangbo4881820.gitee.io/myblog/posts/46420/index.html">
<meta property="og:site_name" content="静待花开">
<meta property="og:description" content="你机器上的docker分为两部分:一个事带有RESTful API的守护线程(daemon)和一个和daemon联系的客户端.下图展示了运行docker客户端和守护线程(daemon)  RESTful API使用标准的HTTP请求方式例如get,post,delete,等等.  您调用Docker客户端以从守护程序获取信息或向守护程序提供指令；守护程序是一个服务器，它使用HTTP协议接收请求并从">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2019-12-18T10:05:47.000Z">
<meta property="article:modified_time" content="2023-09-07T09:44:01.907Z">
<meta property="article:author" content="brad 随风起舞">
<meta property="article:tag" content="容器">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/myblog/img/favicon.png"><link rel="canonical" href="https://zhangbo4881820.gitee.io/myblog/posts/46420/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/myblog/css/index.css"><link rel="stylesheet" href="/myblog/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/myblog/css/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/myblog/',
  algolia: undefined,
  localSearch: {"path":"/myblog/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'docker结构',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2023-09-07 17:44:01'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 7.0.0-rc2"><link rel="alternate" href="/myblog/atom.xml" title="静待花开" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/myblog/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/myblog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a><a href="/myblog/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/myblog/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/myblog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/myblog/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/myblog/" title="静待花开"><span class="site-name">静待花开</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/myblog/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/myblog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/myblog/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">docker结构</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-12-18T10:05:47.000Z" title="发表于 2019-12-18 18:05:47">2019-12-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-09-07T09:44:01.907Z" title="更新于 2023-09-07 17:44:01">2023-09-07</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/myblog/categories/docker/">docker</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="docker结构"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><p>你机器上的docker分为两部分:一个事带有RESTful API的守护线程(daemon)和一个和daemon联系的客户端.下图展示了运行docker客户端和守护线程(daemon)</p>
<blockquote>
<p>RESTful API使用标准的HTTP请求方式例如get,post,delete,等等.</p>
</blockquote>
<p>您调用Docker客户端以从守护程序获取信息或向守护程序提供指令；守护程序是一个服务器，它使用HTTP协议接收请求并从客户端返回响应。反过来，它将使用HTTP协议向其他服务发出发送和接收镜像的请求.服务器从命令行客户端或任何其他被授权的连接去接收请求.守护程序也负责在后台保护你的镜像和容器,然后客户端扮演的是在你和RESTful API之间的中间人</p>
<p><img src="/myblog/image/dockerrunringarchitecture.png"></p>
<p>私有Docker注册表是一项存储Docker镜像的服务。可以从具有相关访问权限的任何Docker守护程序请求这些文件。该注册表位于内部网络上，不能公开访问，因此被视为私有注册表。</p>
<p>您的主机通常将位于专用网络上。如果需要，Docker守护程序将调出互联网以检索镜像。</p>
<p>Docker Hub是由Docker Inc.运行的公共注册表。互联网上也可以存在其他公共注册表，并且您的Docker守护程序可以与它们进行交互。我们说过Docker容器可以运送到可以运行Docker的任何地方-严格来说并非如此。实际上，只有在可以安装守护程序的情况下，容器才会在计算机上运行。</p>
<p>从上图得出的关键点是，当您在机器上运行Docker时，您可能正在与机器上的其他进程进行交互，甚至与网络或Internet上运行的服务进行交互。</p>
<p>现在您已经了解了Docker的布局方式，我们将介绍与图中不同部分有关的各种技术。</p>
<h1 id="docker-daemon"><a href="#docker-daemon" class="headerlink" title="docker daemon"></a>docker daemon</h1><p>docker 守护程序是你和docker交互的中心.他控制在你机器上对docker的访问,管理容器和镜像状态,作为与外部时间交互的代理.</p>
<blockquote>
<p>守护程序是一个运行在后台的进程而不是在用户的直接控制下.服务进程从客户端获取请求并且执行要求的动作去完成请求.daemon也是一个从客户端频繁接收请求和处理请求的服务.docker 命令就是客户端,docker守护程序就是在你的docker容器和镜像上执行的服务</p>
</blockquote>
<p><strong>两种技术</strong></p>
<p>第一种技术允许其他人连接到您的Docker守护程序并执行您可能在主机上执行的相同操作，<br>第二种技术说明Docker容器是由该守护程序而不是您的Shell会话管理的。</p>
<h2 id="第一种开放你的docker-daemon"><a href="#第一种开放你的docker-daemon" class="headerlink" title="第一种开放你的docker daemon"></a>第一种开放你的docker daemon</h2><p>尽管默认情况下，您只能在主机上访问Docker守护程序，但是有充分的理由允许其他人访问它。您可能有一个问题，希望某人进行远程调试，或者您可能希望允许DevOps工作流程的另一部分启动主机上的进程。</p>
<blockquote>
<p>警告 :尽管这是一种强大且有用的技术，但被认为是不安全的。具有访问权限的任何人（包括带有已安装Docker套接字的容器）均可利用Docker套接字获取root特权。</p>
</blockquote>
<p><font color="476B85" style="font-weight: bold;">问题</font></p>
<p>想为其他人开放docker服务的访问权限</p>
<p><font color="476B85" style="font-weight: bold;">解决</font></p>
<p>使用一个开放的TCP地址开始docker守护程序.如下图展示该技术的工作原理:</p>
<p><img src="/myblog/image/dockeropenTCPforother.png"></p>
<p>在打开Docker守护程序之前，必须首先关闭正在运行的守护程序。具体操作方式取决于您的操作系统.如果不确定如何执行此操作，请从以下命令开始</p>
<pre><code>sudo service docker stop
或者
systemctl stop docker
</code></pre>
<p>如果起作用了那么下面的语句将不会看到任务输出:</p>
<pre><code>ps -ef | grep -E &#39;docker(d| -d| daemon)\b&#39; | grep -v grep
</code></pre>
<p>然后就可以用下面命令重启docker并开放给其他人.</p>
<pre><code>sudo docker daemon -H tcp://0.0.0.0:2375
</code></pre>
<p>这个命令启动一个deamon程序,使用<code>-H</code>标志来定义主机服务,使用TCP协议,开放所有IP接口(使用0.0.0.0),开放标准docker服务端口2375<br>这个命令已经过期.可以通过配置文件来改.编辑 <code>/lib/systemd/system/docker.service</code><br>把<code>ExecStart=/usr/bin/dockerd-current \</code> 改为 <code>ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \</code>，如下：</p>
<pre><code>ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
      --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
      --default-runtime=docker-runc \
      --exec-opt native.cgroupdriver=systemd \
      --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
      --init-path=/usr/libexec/docker/docker-init-current \
      --seccomp-profile=/etc/docker/seccomp.json \
      $OPTIONS \
      $DOCKER_STORAGE_OPTIONS \
      $DOCKER_NETWORK_OPTIONS \
      $ADD_REGISTRY \
      $BLOCK_REGISTRY \
      $INSECURE_REGISTRY \
      $REGISTRIES
</code></pre>
<p>然后重新加载配置并重启</p>
<pre><code>systemctl daemon-reload &amp;&amp; systemctl restart docker
</code></pre>
<p>外部环境访问此docker命令</p>
<pre><code>docker -H tcp://192.168.121.131:2375 images
</code></pre>
<p>如果不想每次都写tcp</p>
<pre><code>export DOCKER_HOST=&quot;tcp://192.168.121.131:2375&quot;
然后就可以不用写了
docker images
</code></pre>
<p>说了很多,不过那个命令过期了.可以通过直接修改配置文件来处理哦</p>
<blockquote>
<p>修改&#x2F;etc&#x2F;docker&#x2F;daemon.json文件，如果没有在该目录创建即可，然后添加hosts<br>“registry-mirrors”: [“<a href="https://51qoj676.mirror.aliyuncs.com"]">https://51qoj676.mirror.aliyuncs.com&quot;]</a>,<br>“hosts”: [“tcp:&#x2F;&#x2F;0.0.0.0:2375”,”unix:&#x2F;&#x2F;&#x2F;var&#x2F;run&#x2F;docker.sock”]<br>镜像仓库也能往里面写.<br>然后就是<code>systemctl daemon-reload &amp;&amp; systemctl restart docker</code></p>
</blockquote>
<h2 id="以守护程序方式运行容器"><a href="#以守护程序方式运行容器" class="headerlink" title="以守护程序方式运行容器"></a>以守护程序方式运行容器</h2><p><font color="476B85" style="font-weight: bold;">问题</font></p>
<p>想运行docker容器作为一个后台服务</p>
<p><font color="476B85" style="font-weight: bold;">解决</font></p>
<p>使用<code>-d</code>标志</p>
<pre><code>docker run -d -i -p 1234:1234 --name daemon ubuntu:14.04 nc -l 1234
</code></pre>
<p>写了-d标志就会在后台运行<code>-i</code>标志赋予容器与你的网络会话交互的能力,<code>-p</code>从容器1234端口发布到主机1234端口上.<code>--name</code>给容器命名.<br>最后，使用netcat（nc）在端口1234上运行一个简单的监听回声服务器</p>
<p>现在，您可以连接到它并使用Telnet发送消息。您会看到该容器已使用docker logs命令接收到该消息，如下:</p>
<ol>
<li><p>首先连接容器端口并输入</p>
<blockquote>
<p>telnet localhost 1234<br>Trying ::1…<br>Connected to localhost.<br>Escape character is ‘^]’.<br>hello daemon    (这是咱们输入的)<br>^]  		(按ctrl-]退出telnet会话)</p>
</blockquote>
</li>
<li><p>退出telnet会话并查看容器日志</p>
<blockquote>
<p>telnet&gt; q<br>Connection closed.<br>$ docker logs daemon<br>hello daemon<br>$ docker rm daemon</p>
</blockquote>
</li>
</ol>
<p><font color="476B85" style="font-weight: bold;">docker run–restart</font> 标志允许你应用一系列规则当容器停止时候</p>
<table>
<thead>
<tr>
<th>策略</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>no</td>
<td>当容器存在不会重启,默认的</td>
</tr>
<tr>
<td>always</td>
<td>当容器存在总是重启</td>
</tr>
<tr>
<td>unless-stopped</td>
<td>具有清晰的停止,才会重启</td>
</tr>
<tr>
<td>on-failure[:max-retry]</td>
<td>只有失败时重启</td>
</tr>
</tbody></table>
<p><code>docker run -d --restart=always ubuntu echo done</code></p>
<p>这个命令以后台方式运行容器并且当容器终止的时候always重启.请注意，STATUS列还通知我们该容器在不到一秒钟前退出并正在重新启动。<br>这是因为echo done命令会立即退出，并且Docker必须不断重启容器</p>
<p>请注意这个时候要停止该容器需要下面这么操作:</p>
<blockquote>
<p>docker update –restart no &lt;容器id&gt;</p>
</blockquote>
<h2 id="把docker移动到一个不同的区域"><a href="#把docker移动到一个不同的区域" class="headerlink" title="把docker移动到一个不同的区域"></a>把docker移动到一个不同的区域</h2><p>docker存储所有与你的容器镜像相关的数据在一个文件下.因为它可以存储大量不同的图像，所以该文件夹可以快速变大！</p>
<p>如果主机具有不同的分区（这在企业Linux工作站中很常见），则可能会更快遇到空间限制。在这些情况下，您可能想要移动Docker操作的目录。</p>
<p><font color="476B85" style="font-weight: bold;">问题</font></p>
<p>想要移动dockers存储数据的地方</p>
<p><font color="476B85" style="font-weight: bold;">解决</font></p>
<p>停止和启动dockers服务,通过-g标志指定一个新位置</p>
<blockquote>
<p>dockerd –data-root &#x2F;home&#x2F;dockeruser&#x2F;mydocker</p>
</blockquote>
<h1 id="docker-客户端"><a href="#docker-客户端" class="headerlink" title="docker 客户端"></a>docker 客户端</h1><h2 id="使用socat监控Docker-API流量"><a href="#使用socat监控Docker-API流量" class="headerlink" title="使用socat监控Docker API流量"></a><font color="476B85" style="font-weight: bold;">使用socat监控Docker API流量</font></h2><p><font color="476B85" style="font-weight: bold;">问题</font></p>
<p>你想要使用docker命令调式问题</p>
<p><font color="476B85" style="font-weight: bold;">解决</font><br>使用流量监听器来检查API调用并自己制作。<br>使用这种技术，您将在请求和服务器的套接字之间插入一个代理Unix域套接字，以查看通过它的内容</p>
<p><img src="/myblog/image/dockermonitor.png"></p>
<p>使用<code>socat</code>创建代理</p>
<blockquote>
<p><code>socat</code>是一个功能强大的命令，可让您在几乎任何类型的两个数据通道之间中继数据。如果您熟悉netcat，可以将其视为类固醇上的netcat。<br>要安装它，请使用系统的标准软件包管理器。<br>socat -v UNIX-LISTEN:&#x2F;tmp&#x2F;dockerapi.sock,fork \ UNIX-CONNECT:&#x2F;var&#x2F;run&#x2F;docker.sock &amp;</p>
</blockquote>
<p><code>-v</code>让输出可读,并带有数据流的指示,<code>UNIX-LISTEN</code>部分告诉socat在Unix套接字上侦听，<code>fork</code>确保socat在第一个请求后不退出，而<code>UNIX-CONNECT</code>告诉socat连接到Docker的Unix套接字。＆表示命令在后台运行。</p>
<p>您对守护程序的请求将行进的新路线如图2.6所示。除了Docker客户端提供的任何输出外，socat还将看到在每个方向上传播的所有流量并将其记录到您的终端。</p>
<p><img src="/myblog/image/unixsocket.png"></p>
<h2 id="var-run-docker-sock"><a href="#var-run-docker-sock" class="headerlink" title="&#x2F;var&#x2F;run&#x2F;docker.sock"></a><font color="476B85" style="font-weight: bold;">&#x2F;var&#x2F;run&#x2F;docker.sock</font></h2><p>这东西得专门讲下:</p>
<p>他是docker守护进程默认监听得unix域套接字,容器中得进程可以通过它和docker守护进程通信.</p>
<p>举例子:</p>
<p>看一下<code>Portainer</code>,它提供了图形化界面用于管理docker主机和Swarm集群.如果用<code>Portainer</code>管理本地docker主机得话,就需要绑定**&#x2F;var&#x2F;run&#x2F;docker.sock**</p>
<blockquote>
<p>docker run -dp 9000:9000 -v “&#x2F;var&#x2F;run&#x2F;docker.sock:&#x2F;var&#x2F;run&#x2F;docker.sock” portainer&#x2F;portainer</p>
</blockquote>
<p>这样启动后,就可以通过Portainer绑定得<code>/var/run/docker.sock</code>与守护进程通信,执行各种操作.</p>
<p><font color="476B85" style="font-weight: bold;">Docker守护进程API</font><br>安装docker后,守护进程会监听Unix域套接字:<code>/var/run/docker.sock</code> 这一点可以通过docker deamon得配置文件看出.</p>
<h2 id="在浏览器中使用dockers"><a href="#在浏览器中使用dockers" class="headerlink" title="在浏览器中使用dockers"></a>在浏览器中使用dockers</h2><pre><code>git clone https://github.com/aidanhs/Docker-Terminal.git 
cd Docker-Terminal
&lt;!-- 一定要用python2 --&gt;
py -2 -m SimpleHTTPServer 8000
</code></pre>
<p>不过这玩意要求版本不高于1.9.x .所以不用鸟它.用Portainer就挺好</p>
<h2 id="同一个容器端口运行多个容器"><a href="#同一个容器端口运行多个容器" class="headerlink" title="同一个容器端口运行多个容器"></a>同一个容器端口运行多个容器</h2><blockquote>
<p>docker run -d -p 10001:80 –name blog1 tutum&#x2F;wordpress<br>docker run -d -p 10002:80 –name blog1 tutum&#x2F;wordpress</p>
</blockquote>
<p>这样就在容器80端口上跑了两个博客应用端口分别为10001,10002</p>
<h2 id="支持容器之间交流而外部又访问不来"><a href="#支持容器之间交流而外部又访问不来" class="headerlink" title="支持容器之间交流而外部又访问不来"></a>支持容器之间交流而外部又访问不来</h2><blockquote>
<p>首先使用下面命令创建新的虚拟网络,使用它来进行管理容器通信<br>docker network create my_network<br>默认情况下,使用这个网络连接的所有容器能够通过容器名字对其他容器通信<br>docker network connect my_network blog1<br>docker run -it –network my_network ubuntu:16.04 bash<br>然后这两容器能够互相通信.</p>
</blockquote>
<h2 id="连接独立端口的容器"><a href="#连接独立端口的容器" class="headerlink" title="连接独立端口的容器"></a>连接独立端口的容器</h2><p>你可能想要容器之间通信而不使用自定义的网络</p>
<p>我们将MySQL数据库层与WordPress容器分离，并在不进行端口配置或创建网络的情况下将它们彼此链接</p>
<blockquote>
<p>docker run –name wp-mysql -e MYSQL_ROOT_PASSWORD&#x3D;111111 -d mysql<br>docker run –name wordpress –link wp-mysql:mysql -p 10003:80 -d wordpress</p>
</blockquote>
<h2 id="docker注册中心"><a href="#docker注册中心" class="headerlink" title="docker注册中心"></a>docker注册中心</h2><p>一旦你创建镜像,你可能想要和其他人分享.这个注册中心就是用来分享的. 看下图理解</p>
<p><img src="/myblog/image/dockerregistry.png"></p>
<p>一个docker 注册中心允许多个用户push和pull镜像从中央仓库使用rest api</p>
<h2 id="配置一个本地的docker登记中心"><a href="#配置一个本地的docker登记中心" class="headerlink" title="配置一个本地的docker登记中心"></a>配置一个本地的docker登记中心</h2><p>在本地机器配置一个登记中心,最明显的问题就是会占用机器大量的硬盘空间</p>
<blockquote>
<p>docker run -d -p 5000:5000 -v $HOME&#x2F;registry:&#x2F;var&#x2F;lib&#x2F;registry registry:2</p>
</blockquote>
<p>这命令使得registry在5000端口上可用.使用<code>-v</code>标志，它将使主机<code>（/var/lib/registry）</code>上的注册表文件夹在容器中作为<code>$HOME/registry</code>可用。因此，注册表文件将存储在主机上的&#x2F;var&#x2F;lib&#x2F;registry文件夹中。</p>
<p>现在，您可以发出以下命令：<code>docker push HOSTNAME：5000/image：tag</code>。如您所见，本地注册表的最基本配置级别非常简单，所有数据都存储在$HOME&#x2F;registry目录中。</p>
<p>如果您想扩大规模或使其更强大，则GitHub(<a href="https://github.com/docker/distribution/blob/v2.2.1/docs/">https://github.com/docker/distribution/blob/v2.2.1/docs/</a> storagedrivers.md)上的存储库概述了一些选项，例如在Amazon S3中存储数据。</p>
<h2 id="The-Docker-Hub"><a href="#The-Docker-Hub" class="headerlink" title="The Docker Hub"></a>The Docker Hub</h2><p>这玩意基本就相当于github了.默认咱们的都是从这上面搜索镜像.</p>
<p>直接来代码 ,不罗嗦:</p>
<blockquote>
<p>docker search wordpress<br>NAME                                     DESCRIPTION                                  STARS         OFFICIAL            AUTOMATED<br>wordpress                                The WordPress rich content management system…   3280          [OK]<br>bitnami&#x2F;wordpress                        Bitnami Docker Image for WordPress              125                                     [OK]<br>appcontainers&#x2F;wordpress                  Centos&#x2F;Debian Based Customizable Wordpress C…   34    </p>
</blockquote>
<p>都是按星数STARS排列,OFFICIAL代表被Docker组织信任的,其他按字面意思理解.</p>
<blockquote>
<p>docker pull wordpress<br>这样就把镜像拉到本地了</p>
</blockquote>
<p>(<a href="http://hub.docker.com/">http://hub.docker.com</a>) 这是镜像网址</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://zhangbo4881820.gitee.io/myblog">brad 随风起舞</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhangbo4881820.gitee.io/myblog/posts/46420/">https://zhangbo4881820.gitee.io/myblog/posts/46420/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhangbo4881820.gitee.io/myblog" target="_blank">静待花开</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/myblog/tags/%E5%AE%B9%E5%99%A8/">容器</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/myblog/posts/36057/" title="docker开发"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">docker开发</div></div></a></div><div class="next-post pull-right"><a href="/myblog/posts/20082/" title="CSS01"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">CSS01</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/myblog/posts/36057/" title="docker开发"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-20</div><div class="title">docker开发</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/myblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">brad 随风起舞</div><div class="author-info__description">有些事情需要一辈子的沉淀</div></div><div class="card-info-data site-data is-center"><a href="/myblog/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/myblog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a><a href="/myblog/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/xxxxx" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:xxxxxx@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#docker-daemon"><span class="toc-number">1.</span> <span class="toc-text">docker daemon</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E7%A7%8D%E5%BC%80%E6%94%BE%E4%BD%A0%E7%9A%84docker-daemon"><span class="toc-number">1.1.</span> <span class="toc-text">第一种开放你的docker daemon</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A5%E5%AE%88%E6%8A%A4%E7%A8%8B%E5%BA%8F%E6%96%B9%E5%BC%8F%E8%BF%90%E8%A1%8C%E5%AE%B9%E5%99%A8"><span class="toc-number">1.2.</span> <span class="toc-text">以守护程序方式运行容器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8A%8Adocker%E7%A7%BB%E5%8A%A8%E5%88%B0%E4%B8%80%E4%B8%AA%E4%B8%8D%E5%90%8C%E7%9A%84%E5%8C%BA%E5%9F%9F"><span class="toc-number">1.3.</span> <span class="toc-text">把docker移动到一个不同的区域</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#docker-%E5%AE%A2%E6%88%B7%E7%AB%AF"><span class="toc-number">2.</span> <span class="toc-text">docker 客户端</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8socat%E7%9B%91%E6%8E%A7Docker-API%E6%B5%81%E9%87%8F"><span class="toc-number">2.1.</span> <span class="toc-text">使用socat监控Docker API流量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#var-run-docker-sock"><span class="toc-number">2.2.</span> <span class="toc-text">&#x2F;var&#x2F;run&#x2F;docker.sock</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9C%A8%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%AD%E4%BD%BF%E7%94%A8dockers"><span class="toc-number">2.3.</span> <span class="toc-text">在浏览器中使用dockers</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8C%E4%B8%80%E4%B8%AA%E5%AE%B9%E5%99%A8%E7%AB%AF%E5%8F%A3%E8%BF%90%E8%A1%8C%E5%A4%9A%E4%B8%AA%E5%AE%B9%E5%99%A8"><span class="toc-number">2.4.</span> <span class="toc-text">同一个容器端口运行多个容器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%94%AF%E6%8C%81%E5%AE%B9%E5%99%A8%E4%B9%8B%E9%97%B4%E4%BA%A4%E6%B5%81%E8%80%8C%E5%A4%96%E9%83%A8%E5%8F%88%E8%AE%BF%E9%97%AE%E4%B8%8D%E6%9D%A5"><span class="toc-number">2.5.</span> <span class="toc-text">支持容器之间交流而外部又访问不来</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E7%8B%AC%E7%AB%8B%E7%AB%AF%E5%8F%A3%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-number">2.6.</span> <span class="toc-text">连接独立端口的容器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#docker%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83"><span class="toc-number">2.7.</span> <span class="toc-text">docker注册中心</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E4%B8%80%E4%B8%AA%E6%9C%AC%E5%9C%B0%E7%9A%84docker%E7%99%BB%E8%AE%B0%E4%B8%AD%E5%BF%83"><span class="toc-number">2.8.</span> <span class="toc-text">配置一个本地的docker登记中心</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#The-Docker-Hub"><span class="toc-number">2.9.</span> <span class="toc-text">The Docker Hub</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/15695/" title="jenkins（1）">jenkins（1）</a><time datetime="2023-09-14T02:17:44.000Z" title="发表于 2023-09-14 10:17:44">2023-09-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/22012/" title="wsl的Ubuntu子系统（1）">wsl的Ubuntu子系统（1）</a><time datetime="2023-09-13T08:42:19.000Z" title="发表于 2023-09-13 16:42:19">2023-09-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/30302/" title="effective-java-(接口优于抽象类)">effective-java-(接口优于抽象类)</a><time datetime="2023-09-12T08:12:48.000Z" title="发表于 2023-09-12 16:12:48">2023-09-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/9473/" title="effective-java(创建和销毁对象--组合的形式优于继承)">effective-java(创建和销毁对象--组合的形式优于继承)</a><time datetime="2023-09-11T08:23:19.000Z" title="发表于 2023-09-11 16:23:19">2023-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/16107/" title="Hello World">Hello World</a><time datetime="2023-09-07T09:44:01.910Z" title="发表于 2023-09-07 17:44:01">2023-09-07</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By brad 随风起舞</div><div class="framework-info"><span>框架 </span><a href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/myblog/js/utils.js"></script><script src="/myblog/js/main.js"></script><script src="/myblog/js/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/myblog/js/search/local-search.js"></script></div></div></body></html>